package com.ct.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ct.entity.po.Travel;
import com.ct.entity.query.TravelQuery;
import com.ct.entity.vo.ResponseVO;
import com.ct.factory.TravelFactory;
import com.ct.service.TravelService;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;

/**
 * 出差表 Controller
 */
@RestController("travelController")
@RequestMapping("/travel")
public class TravelController extends ABaseController {

    @Resource
    private TravelService travelService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadDataList")
    public ResponseVO loadDataList(TravelQuery query) {
        return getSuccessResponseVO(travelService.findListByPage(query));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public ResponseVO add(@NotEmpty String travelReason, @NotEmpty String travelDetail, @NotEmpty Date applyDate,
            @NotEmpty Date startDate, @NotEmpty Date endDate, @NotEmpty BigDecimal foodCost,
            @NotEmpty BigDecimal transportCost, @NotEmpty BigDecimal hotelCost,
            @NotEmpty String costDetail, @NotEmpty BigDecimal advancePayment, @NotEmpty String userId) {
        Travel bean = TravelFactory.createTravel(travelReason, travelDetail, applyDate, startDate, endDate, foodCost,
                transportCost, hotelCost, costDetail, advancePayment, userId);
        boolean isSucceed = travelService.add(bean) > 0;

        System.out.println("Exiting add method...");
        System.out.println(isSucceed);
        if (isSucceed) {
            String queueName = "travelApply.queue";
            Map<String, Object> map = new HashMap<>();
            map.put("message", bean.getUserId() + "已提交出差记录，请及时处理");
            System.out.println("已提交出差记录，请及时处理");
            rabbitTemplate.convertAndSend(queueName, map);
        }

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/addTravel")
    public String addTravel(@RequestBody Travel bean) {
        System.out.println(bean);
        System.out.println("Entering add method...");
        boolean isSucceed = travelService.addTravel(bean) > 0;
        if (isSucceed) {
            String queueName = "travelApply.queue";
            Map<String, Object> map = new HashMap<>();
            map.put("message", bean.getUserId() + "已提交出差记录，请及时处理");
            System.out.println("已提交出差记录，请及时处理");
            rabbitTemplate.convertAndSend(queueName, map);
        }
        System.out.println(isSucceed);
        return isSucceed ? "yes" : "no";
    }

    /**
     * 批量新增
     */
    @RequestMapping("/addBatch")
    public ResponseVO addBatch(@RequestBody List<Travel> listBean) {
        travelService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增/修改
     */
    @RequestMapping("/addOrUpdateBatch")
    public ResponseVO addOrUpdateBatch(@RequestBody List<Travel> listBean) {
        travelService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据TravelId查询对象
     */
    @RequestMapping("/getTravelByTravelId")
    public ResponseVO getTravelByTravelId(Integer travelId) {
        return getSuccessResponseVO(travelService.getTravelByTravelId(travelId));
    }

    /**
     * 根据TravelId修改对象
     */
    @RequestMapping("/updateTravelByTravelId")
    public ResponseVO updateTravelByTravelId(Travel bean, Integer travelId) {
        travelService.updateTravelByTravelId(bean, travelId);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据TravelId删除
     */
    @RequestMapping("/deleteTravelByTravelId")
    public ResponseVO deleteTravelByTravelId(Integer travelId) {
        travelService.deleteTravelByTravelId(travelId);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/getAllTravelsByIdList")
    public List<Travel> getAllTravelsByIdList(@RequestBody List<String> travelIdList) {
        return travelService.getAllTravelsByIdList(travelIdList);
    }

    @RequestMapping("/getAllTravelListByUserId")
    public ResponseVO getAllTravelListByUserId(@RequestParam String userId) {
        return getSuccessResponseVO(travelService.getAllTravelListByUserId(userId));
    }

    /*
     * 根据UserId查询travelList（这是筛选后的）
     */
    @RequestMapping("/getFinalTravelListByUserId")
    public ResponseVO getFinalTravelListByUserId(@RequestParam String userId) {
        return getSuccessResponseVO(travelService.findFinalListByUserId(userId));
    }

}
